package org.hxyjs.dao.impl;

import org.hxyjs.dao.RegionDao;
import org.hxyjs.entity.Region;
import org.hxyjs.entity.Region;
import org.hxyjs.entity.Region;
import org.hxyjs.mapper.RegionMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("regionDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class RegionDaoImpl implements RegionDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private RegionMapper regionMapper;
    public List<Region>  getAllRegions() throws Exception{
        return regionMapper.selectByExample(null);
    }

    @Override
    public Integer deleteRegionByRno(String rno)  throws Exception{
        return regionMapper.deleteByPrimaryKey(rno);
    }

    @Override
    public Integer updateRegionByRno(Region region)  throws Exception{
        return regionMapper.updateByPrimaryKey(region);
    }

    @Override
    public Integer addRegion(Region region)  throws Exception{
        region.setRno(initId());
        return regionMapper.insertSelective(region);
    }
    public String initId(){
        String maxRno = regionMapper.getMaxRno();
        return GenNumTools.initId("",3,maxRno);
    }
    @Override
    public List<Region> getRegionByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)regionMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return regionMapper.getRegionByPage(map);
    }
}
